<template>
  <div>
    <el-card>
      <!-- 上面 -->
      <el-row type="flex" justify="space-between">
        <el-col>
          <div class="before">
            <el-button size="small" type="primary" @click="addRole">添加分类</el-button>
          </div>
        </el-col>
        <el-col class="right">
          <el-input
            v-model="input"
            placeholder="请输入关键字"
          />
          <el-button icon="el-icon-search" class="one" @click="onSubmit" />
        </el-col>
      </el-row>
    </el-card>
    <!-- 表单 -->
    <el-table border :data="tableData" row-key="id">
      <el-table-column prop="image" label="图片">
        <template v-slot="{row}">
          <img :src="row.image" alt="">
        </template>
      </el-table-column>
      <el-table-column prop="title" label="标题" />
      <el-table-column prop="desc" label="介绍" />
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button type="primary" size="small">编辑</el-button>
          <el-button
            type="danger"
            size="small"
            @click="delRole(row.id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :page-size="1"
      :pagercount="2"
      layout="prev, pager, next"
      :total="2"
      class="two"
    />
    <Addpage :show-dialog.sync="visibleDialog" />
  </div>
</template>

<script>
import Addpage from './addpage/index.vue'
import { getmanagement, delcategory, querycategory } from '@/api/interface/management'
export default {
  components: { Addpage },
  data() {
    return {
      input: '',
      visibleDialog: false,
      tableData: []
    }
  },
  created() {
    this.getmanagement()
  },
  methods: {
    // 查询
    async onSubmit() {
      const res = await querycategory(this.input)
      console.log('在这里呀', res.data)
      this.tableData = res.data
    },
    // 删除分类
    delRole(id) {
      this.$confirm('确认删除该分类？', '删除', {
        type: 'warning'
      }).then(async() => {
        // console.log('点击了确定按钮')
        // 调用删除接口函数
        await delcategory(id)
        // 更新列表
        this.getmanagement()
      }).catch(() => {
        console.log('点击了取消按钮')
      })
    },
    // 添加分类
    addRole() {
      this.visibleDialog = true
    },
    // 渲染
    async getmanagement() {
      const res = await getmanagement()
      this.tableData = res.data
    }
  }
}
</script>

<style lang="scss">
img{
  width: 40%;
}
.two{
  margin-top: 20px;
  text-align: center;
}
.el-card{
  padding: 20px;
  .right {
  display: flex;
  justify-content: space-between;

  .el-button{
    margin-top: 0;
  }
    .one{
      background-color: #EBEEF5;
    }
  }
}
</style>
